﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;
using BusinessObjects;
using System.IO;

public partial class controls_admin_CategoryEdit : Pages_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            loadSetting();
            base.OpenConnection();
            base.removeHGC(divMessage);
            if (!IsPostBack)
            {
                base.loadCategories(ddlParent, 0);
                if (Request["c_id"] != null)
                    loadData(Convert.ToInt32(Request["c_id"]));
            }
        }
        catch { }
        finally { base.CloseConnection(); }
    }
    protected void loadData(int c_id)
    {
        DataTable objDT = BO_Categories.Search(c_id);
        ddlParent.SelectedValue = objDT.Rows[0]["PARENT_ID"].ToString();
        txtFullCategoryName.Text = objDT.Rows[0]["FULL_CATEGORY_NAME"].ToString();
        txtFullCategoryDescription.Text = objDT.Rows[0]["FULL_CATEGORY_DESCRIPTION"].ToString();
        txtSortDisplay.Text = objDT.Rows[0]["SORT_DISPLAY"].ToString();
        cboDisOnHome.Checked = Convert.ToBoolean(objDT.Rows[0]["IS_DISPLAY_ON_HOME_PAGE"].ToString());
        cboMenu.Checked = Convert.ToBoolean(objDT.Rows[0]["IS_MENU"].ToString());
        if (!objDT.Rows[0]["THUMB_ICON"].ToString().Trim().Equals(String.Empty))
            divIcon.InnerHtml = "<img src='/upload/home/" + objDT.Rows[0]["THUMB_ICON"].ToString() + "' alt='" + objDT.Rows[0]["FULL_CATEGORY_NAME"].ToString() + "' height='100' />";
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        txtFullCategoryDescription.Text = "";
        txtFullCategoryName.Text = "";
        cboDisOnHome.Checked = true;
        cboMenu.Checked = false;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            base.OpenConnection();
            int c_id;
            if (Request["c_id"] == null)
                return;
            c_id = Convert.ToInt32(Request["c_id"]);
            DataTable objDT = BO_Categories.Search(c_id);
            string filename = String.Empty;
            try
            {
                if (fulIcon.HasFile)
                {
                    filename = fulIcon.FileName;
                    int count = 0;
                    while (File.Exists(Server.MapPath("~/upload/home/") + filename))
                    {
                        filename = count + "_" + fulIcon.FileName;
                        count++;
                    }
                    fulIcon.SaveAs(Server.MapPath("~/upload/home/") + filename);
                }
            }
            catch (Exception ex)
            {
                divMessage.InnerHtml = O2S_Message.Error("Hệ Thống", "Lỗi Ghi file! " + ex.Message);
                return;
            }
            if (objDT != null && objDT.Rows.Count > 0)
            {
                DataTable adminDT = (DataTable)Session["Admin"];
                string categoryName = txtFullCategoryName.Text.Trim();
                string categoryDescription = txtFullCategoryDescription.Text.Trim();
                int sortdisplay = Convert.ToInt32(txtSortDisplay.Text.Trim());
                int parent = Convert.ToInt32(ddlParent.SelectedValue);
                string result = BO_Categories.Update(c_id,
                        parent, "",
                        "", categoryName,
                        "", filename, categoryDescription, "", categoryDescription, "",
                        cboDisOnHome.Checked, cboMenu.Checked, sortdisplay,
                        Convert.ToDateTime(objDT.Rows[0]["CREATED_DATE"]),
                        objDT.Rows[0]["CREATED_BY"].ToString(),
                        DateTime.Now, adminDT.Rows[0]["USER_NAME"].ToString(), true);
                if (result.Equals("0"))
                    divMessage.InnerHtml = O2S_Message.Error("Thông Báo", "Chưa thực hiện được, vui lòng trở lại sau !");
                else
                    divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Thực hiện thành công !");
            }
        }
        catch (Exception ex) { divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message); }
        finally { base.CloseConnection(); }
    }
    private void loadSetting()
    {
        DataTable objDT = BO_Settings.SelectByGroupId(8, 1);
        foreach (DataRow dr in objDT.Rows)
        {
            string value = dr["SETTING_VALUE"].ToString();
            switch (dr["SETTING_KEY"].ToString())
            {
                case "NOTE_CATE_CATE":
                    NOTE_CATE_CATE.InnerText = value;
                    break;
                case "NOTE_CATE_DESCRIPT":
                    NOTE_CATE_DESCRIPT.InnerText = value;
                    break;
                case "NOTE_CATE_DISPLAY":
                    NOTE_CATE_DISPLAY.InnerText = value;
                    break;
                case "NOTE_ICON":
                    NOTE_ICON.InnerText = value;
                    break;
                case "NOTE_CATE_MENU":
                    NOTE_CATE_MENU.InnerText = value;
                    break;
                case "NOTE_CATE_SORT":
                    NOTE_CATE_SORT.InnerText = value;
                    break;
                case "NOTE_CATE_TITLE":
                    NOTE_CATE_TITLE.InnerText = value;
                    break;
            }
        }
    }
}
